#ifndef __RayTerrainQuery_H__
#define __RayTerrainQuery_H__

#include "video/Ray.h"
#include "video/Terrain.h"
#include "d3dheaders.h"
#include <vector>

namespace KEngine
{

struct RayTerrainQueryResultEntry
{
	// Distance along the ray
	float distance;
	// Single intersection point
	D3DXVECTOR3 singleIntersection;
	// Comparison operator for sorting
	bool operator < (const RayTerrainQueryResultEntry& rhs) const
	{
		return this->distance < rhs.distance;
	}
};
typedef std::vector<RayTerrainQueryResultEntry> RayTerrainQueryResult;

class RayTerrainQuery
{
public:
	RayTerrainQuery(Terrain* terrain);
	virtual ~RayTerrainQuery();

	virtual void setRay(const Ray& ray);
	virtual const Ray& getRay() const;
	virtual void setSortByDistance(bool sort, unsigned short maxresults = 0);
	virtual bool getSortByDistance() const;
	virtual unsigned short getMaxResults() const;

	virtual RayTerrainQueryResult& execute();
	virtual RayTerrainQueryResult& getLastResults();
	virtual void clearResults();
	virtual bool queryResult(const D3DXVECTOR3& intersection, float distance);

protected:
	Ray						mRay;
	bool					mSortByDistance;
	unsigned short			mMaxResults;
	Terrain*				mParentTerrain;
	RayTerrainQueryResult	mResult;
};

}

#endif //